﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BOHVote.BLL;

namespace BOH.ControlPanel
{
    public partial class SpecialityModify : System.Web.UI.Page
    {
        protected string pageType = string.Empty;
        protected string ID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            pageType = Request.QueryString["type"];
            ID = Request.QueryString["ID"];
            if (!IsPostBack)
            {
                BindDrpSubject();
                if (pageType != null && ID != null && ID.Trim().Length > 0 && pageType == "edit")
                {
                    getSpecialityByID(ID);
                }
            }
        }

        public void BindDrpSubject()
        {
            DataSet dst = Subject.getSubject("", "A");
            drpSubject.DataSource = dst.Tables[0].DefaultView;
            drpSubject.DataTextField = "SubjectName";
            drpSubject.DataValueField = "SubjectID";
            drpSubject.DataBind();
            ListItem listitem = new ListItem("", "");
            listitem.Selected = true;
            drpSubject.Items.Add(listitem);
        }

        public void getSpecialityByID(string ID)
        {
            DataSet dst = Subject.getSpeciality(ID);
            if (dst.Tables[0].Rows.Count > 0)
            {
                this.txtSpecialityName.Text = dst.Tables[0].Rows[0]["SpecialityName"].ToString();
                this.txtSpecialityID.Text = dst.Tables[0].Rows[0]["SpecialityID"].ToString();
                drpStatus.SelectedValue = dst.Tables[0].Rows[0]["Status"].ToString();
                this.drpSubject.SelectedValue = dst.Tables[0].Rows[0]["SubjectID"].ToString();
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            if (pageType != null && ID != null && ID.Trim().Length > 0 && pageType == "edit")
            {
                Subject.UpdateSpeciality(ID,txtSpecialityID.Text,txtSpecialityName.Text,drpSubject.SelectedValue,drpStatus.SelectedValue);
            }
            else
            {
                Subject.InsertSpeciality(txtSpecialityID.Text, txtSpecialityName.Text, drpSubject.SelectedValue, drpStatus.SelectedValue);
            }

            Response.Redirect("SpecialityList.aspx");
        }
    }
}
